﻿namespace H2.Streams.Sections
{
    using H2.Streams;
    using System;

    public class Index
    {
        private MapStream Map;

        internal Index(MapStream map)
        {
            this.Map = map;
            this.Map.TagInfoIndexOffset = this.TagInfoIndexOffset;
            this.Map.TagInfoCount = this.TagCount;
            this.Map.SecondaryMagic = this.SecondaryMagic;
        }

        public int GlobalsID
        {
            get
            {
                return this.Map.ReadInt32At(this.Map.IndexOffset + 0x10, true);
            }
            set
            {
                this.Map.WriteAt(this.Map.IndexOffset + 0x10, value, true);
            }
        }

        public int PrimaryMagic
        {
            get
            {
                return (this.PrimaryMagicConstant - (this.Map.IndexOffset + 0x20));
            }
        }

        public int PrimaryMagicConstant
        {
            get
            {
                return this.Map.ReadInt32At(this.Map.IndexOffset, true);
            }
            set
            {
                this.Map.WriteAt(this.Map.IndexOffset, value, true);
            }
        }

        public int ScnrID
        {
            get
            {
                return this.Map.ReadInt32At(this.Map.IndexOffset + 12, true);
            }
            set
            {
                this.Map.WriteAt(this.Map.IndexOffset + 12, value, true);
                this.Map.Scnr.ID = value;
            }
        }

        public int SecondaryMagic
        {
            get
            {
                return (this.SecondaryMagicConstant - this.Map.MetaStart);
            }
        }

        public int SecondaryMagicConstant
        {
            get
            {
                return this.Map.ReadInt32At(this.Map.TagInfoIndexOffset + 8, true);
            }
            set
            {
                this.Map.WriteAt(this.Map.TagInfoIndexOffset + 8, value, true);
                this.Map.SecondaryMagic = value - this.Map.MetaStart;
            }
        }

        public int TagCount
        {
            get
            {
                return this.Map.ReadInt32At(this.Map.IndexOffset + 0x18, true);
            }
            set
            {
                this.Map.WriteAt(this.Map.IndexOffset + 0x18, value, true);
                this.Map.TagInfoCount = value;
            }
        }

        public int TagInfoIndexOffset
        {
            get
            {
                return (this.TagInfoIndexPointer - this.PrimaryMagic);
            }
        }

        public int TagInfoIndexPointer
        {
            get
            {
                return this.Map.ReadInt32At(this.Map.IndexOffset + 8, true);
            }
            set
            {
                this.Map.WriteAt(this.Map.IndexOffset + 8, value, true);
                this.Map.TagInfoIndexOffset = value - this.PrimaryMagic;
            }
        }

        public string Tags
        {
            get
            {
                return this.Map.ReadStringAt(this.Map.IndexOffset + 0x1c, 4, false, true);
            }
            set
            {
                this.Map.WriteAt(this.Map.IndexOffset + 0x1c, value.Substring(0, 4), true);
            }
        }
    }
}

